package com.example.administrator.demo;

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;

import cn.bmob.v3.Bmob;



public class MainActivity extends AppCompatActivity {
    //新闻页面框架
    private News news = new News();
    private Domestic domestic= new Domestic();
    private Ent ent = new Ent();
    private Internation internation = new Internation();
    private Tech tech = new Tech();
    private Military military = new Military();
    private Game game = new Game();
    private Sports sports = new Sports();
    private Finance finance = new Finance();
    private Society society = new Society();



    //新闻种类索引
    private static final int TAB_INDEX_COUNT = 10;

    private static final int TAB_INDEX_ONE = 0;
    private static final int TAB_INDEX_TWO = 1;
    private static final int TAB_INDEX_THREE = 2;
    private static final int TAB_INDEX_FOUR = 3;
    private static final int TAB_INDEX_FIVE = 4;
    private static final int TAB_INDEX_SIX = 5;
    private static final int TAB_INDEX_SEVEN = 6;
    private static final int TAB_INDEX_EIGHT = 7;
    private static final int TAB_INDEX_NINE = 8;
    private static final int TAB_INDEX_TEN = 9;

    //ViewPager
    private ViewPager mViewPager;
    private ViewPagerAdapter mViewPagerAdapter;

    //PagerTabStrip
    PagerTabStrip pagerTabStrip;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //初始化Bmob
        Bmob.initialize(this, "24e89d3c1e746bd3d1d0f3a3fc14657d");
        setUpViewPager();

    }


    public class ViewPagerAdapter extends FragmentPagerAdapter {
        public ViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case TAB_INDEX_ONE:
                    return news;
                case TAB_INDEX_TWO:
                    return domestic;
                case TAB_INDEX_THREE:
                    return ent;
                case TAB_INDEX_FOUR:
                    return internation;
                case TAB_INDEX_FIVE:
                    return tech;
                case TAB_INDEX_SIX:
                    return military;
                case TAB_INDEX_SEVEN:
                    return game;
                case TAB_INDEX_EIGHT:
                    return sports;
                case TAB_INDEX_NINE:
                    return finance;
                case TAB_INDEX_TEN:
                    return society;
            }
            throw new IllegalStateException("No Fragment!");
        }

        @Override
        public int getCount() {
            return TAB_INDEX_COUNT;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            String tabLabel = null;
            switch (position) {
                case TAB_INDEX_ONE:
                    tabLabel = "今日";
                    break;
                case TAB_INDEX_TWO:
                    tabLabel = "国内";
                    break;
                case TAB_INDEX_THREE:
                    tabLabel = "娱乐";
                    break;
                case TAB_INDEX_FOUR:
                    tabLabel = "国际";
                    break;
                case TAB_INDEX_FIVE:
                    tabLabel = "科技";
                    break;
                case TAB_INDEX_SIX:
                    tabLabel = "军事";
                    break;
                case TAB_INDEX_SEVEN:
                    tabLabel = "游戏";
                    break;
                case TAB_INDEX_EIGHT:
                    tabLabel = "体育";
                    break;
                case TAB_INDEX_NINE:
                    tabLabel = "财经";
                    break;
                case TAB_INDEX_TEN:
                    tabLabel = "社会";
                    break;
            }
            return tabLabel;
        }
    }

    private void setUpViewPager() {
        mViewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());

        mViewPager = (ViewPager) findViewById(R.id.viewpager);
        mViewPager.setAdapter(mViewPagerAdapter);
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


    }
}
